package test.crane;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.*;
import ptolemy.data.type.*;

//////////////////////////////////////////////////////////////////////////
//// Check1
/**

@author Ivan Jeukens
@version v 0.1 07/02/2002
*/
public class CheckPosDesired extends TypedAtomicActor {

    public CheckPosDesired(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        shutdown = new TypedIOPort(this, "shutdown", true, false);
        shutdown.setTypeEquals(BaseType.GENERAL);

        trigger = new TypedIOPort(this, "trigger", true, false);
        trigger.setTypeEquals(BaseType.GENERAL);

        posdesired = new TypedIOPort(this, "posdesired", true, false);
        posdesired.setTypeEquals(BaseType.DOUBLE);
        
        emstop_input = new TypedIOPort(this, "emstop_input", true, false);
        emstop_input.setTypeEquals(BaseType.GENERAL);

        emstop = new TypedIOPort(this, "emstop", false, true);
        emstop.setTypeEquals(BaseType.GENERAL);

        new Attribute(this, "_nonStrictMarker");
    }

    public TypedIOPort trigger;
    public TypedIOPort shutdown;
    public TypedIOPort posdesired;
    public TypedIOPort emstop_input;

    public TypedIOPort emstop;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void pruneDependencies() {
        super.pruneDependencies();
        removeDependency(trigger, emstop);
        removeDependency(shutdown, emstop);
        removeDependency(emstop_input, emstop);
    }

    public void initialize() throws IllegalActionException  {
        super.initialize();
        _reset();
    }

    public void fire() throws IllegalActionException {
        if(_state == RUNNING) {
            if(posdesired.isKnown() && !emstop.isKnown()) {
            //if(posdesired.isKnown()) {
                if(posdesired.hasToken(0)) {
                    DoubleToken db = (DoubleToken) posdesired.get(0);
                    double v = db.doubleValue();
                    if(v < -4.0 || v > 4.0) {
                        emstop.broadcast(new Token());
                        System.out.println("PosDesired : emstop");
                        _reset();
                    }
                }
            }
        }

        if(posdesired.isKnown() && !emstop.isKnown()) {
            emstop.broadcastClear();
        }
    }

    public boolean postfire() throws IllegalActionException {
        switch(_state) {
            case READY : {
                if(trigger.isKnown() && shutdown.isKnown()) {
                    if(trigger.hasToken(0) && !shutdown.hasToken(0)) {
                        _state = RUNNING;
                    }
                }
            } break;

            case RUNNING: {
                if(shutdown.isKnown()) {
                    if(shutdown.hasToken(0)) {
                        _reset();
                        break;
                    }
                }

                if(emstop_input.isKnown()) {
                    if(emstop_input.hasToken(0)) {
                        _reset();
                    }
                }
            } break;
        }
        return true;
    }

    ///////////////////////////////////////////////////////////////////
    ////                        private methods                    ////

    private void _reset() {
        _state = READY;
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

    private int _state;

    private static final int READY = 1;
    private static final int RUNNING = 2;
}
